<?php
class WidgetController extends AdminController {
	public function WidgetController(){
		parent::AdminController();
		
	}
	
	public function index(){
		$this->assign('content_widget_cates', BaseWidget::get_content_widget_cates());
		$this->assign('layout_widget_cates', BaseWidget::get_layout_widget_cates());
		$this->display();
	}
	
	public function cate(){
		if($this->id === 'root' || $this->id === 'layout'){
			$cates = BaseWidget::get_layout_widget_cates();
		}else{
			$cates = BaseWidget::get_content_widget_cates();
		}
		$this->assign('cate', $cates[$this->id]);
		$widget_mod = new WidgetModel();
		$widgets = $widget_mod->find(array('cate'=>$this->id));
// 		$widgets = BaseWidget::get_widgets_by_cate($this->id);
		if($widgets!=null){
			$this->assign('widgets', $widgets);
		}
		$this->display();
	}
	
	public function edit(){
		if(IS_POST){
			self::save();
		}
		$content_widget_cates = BaseWidget::get_content_widget_cates();
		$options = array();
		foreach($content_widget_cates as $k=>$v){
			$options[$k] = $v['name'];
		}
		
		$this->assign('cates', $options);
		
		$this->display();
	}
	
	public function cate_list(){
		$widget_cate_name = 'widget_cate';
		$relation_mod = new RelationModel();
		$widget_cates = $relation_mod->get_relation_by_code($widget_cate_name);
		lang_fatch($widget_cates, LANGUAGE);
		$this->assign('widget_cates', $widget_cates);
		$this->display();
	}
	
	public function cate_edit(){
		if(IS_POST){
			self::cate_save();
		}
		if(!empty($this->id)){
			$widget_cate_name = 'widget_cate';
			$relation_mod = new RelationModel();
			$widget_cate = $relation_mod->get_by_attrs(array('code'=>$widget_cate_name), $this->id);
			$widget_cate = $widget_cate[$this->id];
			$widget_cate['_id'] = $this->id;
			lang_fatch($widget_cate, LANGUAGE);
			$this->assign('cate', $widget_cate);
		}
		$this->display();
	}
	
	private function cate_save(){
		$widget_cate_name = 'widget_cate';
		if(empty($this->id)){
			self::ajax_error('分类编码不能为空！');
		}
		if (!preg_match('/[[a-z|A-Z][a-z|A-Z|0-9|_]+/i', $this->id)) {
			self::ajax_error('分类编码不合法！');
		}
		$name = self::get_post_val('name');
		if(empty($name) || utf8_strlen($name)>50){
			self::ajax_error('分类名称不正确！');
		}
		$desc = self::get_post_val('desc');
		$data = array(
			'name' => $name,
			'desc' => $desc,
		);
		$relation_mod = new RelationModel();
		$relation_mod->update_by_attrs(array('code'=>$widget_cate_name), array($this->id=>array(LANGUAGE=>$data)));
		self::ajax_info('保存成功！');
	}
	
	public function cate_del(){
		$relation_mod = new RelationModel();
		if(empty($this->id)){
			self::ajax_error('未指定要删除的记录！');
		}
		$widget_cate_name = 'widget_cate';
		$relation_mod->delete_column(array('code'=>$widget_cate_name), $this->id);
		self::ajax_info('删除成功！');
	}
	
	private function save(){
		$form_id = self::get_post_val('form_id');
		$h = new AttrHelper(array('id'=>$form_id));
		$post_data = $h->save();
		if($post_data['result']){
			//保存成功
			self::create_widget_file($post_data['data']);
			
			self::ajax_info($post_data['info']);
		}else{
			//有错误
			self::ajax_error($post_data['info']);
		}
	}
	
	private function create_widget_file($widget_data){
		$widget_base_path = ROOT_PATH . '/protected/includes/widgets/';
		$code = $widget_data['code'];
		$cate = $widget_data['cate'];
		$widget_path = $widget_base_path . $cate . '/' . $code . '/';
		if(!is_dir($widget_path . 'template')){
			yu_mkdirs($widget_path . 'template');
		}
		$widget_class = ucfirst($cate) . ucfirst($code) . 'Widget.php';
		if(!is_file($widget_path . $widget_class)){
			//创建组件类
			file_put_contents($widget_path . $widget_class, self::gen_widget_class_content($widget_data));
		}
		
		if(isset($widget_data['templates'])){
			foreach ($widget_data['templates'] as $template){
				//模板目录
				$template_file = $template['template_file']. '.' . TEMPLATE_TYPE;
				if(!is_file($widget_path . 'template/' . $template_file)){
					//创建模板文件
					file_put_contents($widget_path . 'template/' . $template_file, '');
				}
			}
		}
		
	}
	
	private function gen_widget_class_content($widget_data){
		$cate = $widget_data['cate'];
		$code = $widget_data['code'];
		return 
'<?php
class ' . ucfirst($cate) . ucfirst($code) . 'Widget extends BaseWidget{
	/**
	 * 显示前获取需要显示的数据
	 */
	public function get_data(){
		
		return array();
	}
	
	/**
	 * 配置时处理Post数据
	 * @param $data 封装Post数据的数组
	 */	
	public function get_post_data(&$data){
		//$data[\'name\'] = trim($_POST[\'name\']);
		
	}
}
?>';
	}
	
	public function help(){
		$this->display();
	}
}

?>